/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.controllers.ui;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.toedo.Activator;
import org.toedo.gui.actions.NewNoteAction;
import org.toedo.gui.actions.NewProjectAction;
import org.toedo.gui.actions.NewTaskAction;
import org.toedo.gui.actions.SaveProjectAction;
import org.toedo.gui.controlcontributions.NewBaseNoteContribution;

public class ToolbarController {

	/**
	 * Holder for singleton implementation.
	 */
	private static class ToolbarControllerHolder {
		private static final ToolbarController INSTANCE = new ToolbarController();
	}
	
	/**
	 * Get the unique instance of the Controller.
	 * @return The instance of the Controller
	 */
	public static ToolbarController getInstance() {
		return ToolbarControllerHolder.INSTANCE;
	}
	
	private ToolbarController() {
		
	}
	
	private IAction mNewProjectAction;
	private IAction mSaveProjectAction;
	
	private NewBaseNoteContribution mNewTaskContribution;
	private IAction mNewTaskAction;
	
	private NewBaseNoteContribution mNewNoteContribution;
	private IAction mNewNoteAction;
	
	public void makeActions() {
		mNewProjectAction = new Action("New project", SWT.NONE) {
			public void run() {
				new NewProjectAction(Display.getCurrent().getActiveShell()).run();
			}
		};
		mNewProjectAction.setImageDescriptor(Activator.getImageDescriptor("/icons/new-16.png"));
		
		mSaveProjectAction = new Action("Save project (CTRL + S)", SWT.NONE) {
			public void run() {
				new SaveProjectAction(false).run();
			}
		};
		mSaveProjectAction.setImageDescriptor(Activator.getImageDescriptor("/icons/save-16.png"));
	}
	
	public void fillToolbar(ICoolBarManager coolBar) {
		IToolBarManager controlToolbar1 = new ToolBarManager(coolBar.getStyle());
		coolBar.add(controlToolbar1);
		
		controlToolbar1.add(mNewProjectAction);
		controlToolbar1.add(mSaveProjectAction);

		controlToolbar1.update(true);
		
		IToolBarManager controlToolbar2 = new ToolBarManager(coolBar.getStyle());
		coolBar.add(controlToolbar2);

		mNewTaskContribution = new NewBaseNoteContribution("New task:") {
			@Override
			public void processAction() {
				String value = mInputText.getText();
				if ((value != null) &&
						(!value.isEmpty())) {
					new NewTaskAction(Display.getCurrent().getActiveShell(), value, true).run();
					mInputText.setText(""); //$NON-NLS-1$
				}
			}			
		};		
		controlToolbar2.add(mNewTaskContribution);
		
		mNewTaskAction = new Action("Add task", SWT.NONE) {
			public void run() {
				mNewTaskContribution.processAction();
			}
		};
		mNewTaskAction.setImageDescriptor(Activator.getImageDescriptor("/icons/add-16.png"));
		controlToolbar2.add(mNewTaskAction);
		
		controlToolbar2.update(true);
		
		IToolBarManager controlToolbar3 = new ToolBarManager(coolBar.getStyle());
		coolBar.add(controlToolbar3);

		mNewNoteContribution = new NewBaseNoteContribution("New note:") {
			@Override
			public void processAction() {
				String value = mInputText.getText();
				if ((value != null) &&
						(!value.isEmpty())) {
					new NewNoteAction(Display.getCurrent().getActiveShell(), value, true).run();
					mInputText.setText(""); //$NON-NLS-1$
				}
			}			
		}; 		
		controlToolbar3.add(mNewNoteContribution);
		
		mNewNoteAction = new Action("Add note", SWT.NONE) {
			public void run() {
				mNewNoteContribution.processAction();
			}
		};
		mNewNoteAction.setImageDescriptor(Activator.getImageDescriptor("/icons/add-16.png"));
		controlToolbar3.add(mNewNoteAction);
		
		controlToolbar3.update(true);
	}
	
}
